package com.practice.car.cardataapp.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.practice.car.cardataapp.entity.NationalCarSalesTrends;
import com.practice.car.cardataapp.entity.frontEnd.Form;
import com.practice.car.cardataapp.mapper.NationalCarSalesTrendsDao;
import com.practice.car.cardataapp.service.NationalCarSalesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * car-data-app project
 *
 * @author ZhangMingXing
 * @date 2020/9/6
 */
@Service
public class NationalCarSalesServiceImpl extends ServiceImpl<NationalCarSalesTrendsDao,NationalCarSalesTrends> implements NationalCarSalesService {
	@Override
	public List<Form> getSeries(String year) {
		List<NationalCarSalesTrends> list = this.list();
		list.sort((a,b)->{
			String[] aStrings = a.getMonth().split("-");
			String[] bStrings = b.getMonth().split("-");
			Integer aYear = Integer.valueOf(aStrings[0]);
			Integer bYear = Integer.valueOf(bStrings[0]);
			if (!Objects.equals(aYear, bYear)) {
				return aYear - bYear;
			} else {
				Integer aMonth = Integer.valueOf(aStrings[1]);
				Integer bMonth = Integer.valueOf(bStrings[1]);
				return aMonth - bMonth;
			}
		});
		ArrayList<NationalCarSalesTrends> thisYearList = new ArrayList<>();
		list.forEach(c->{
			if (c.getMonth().contains(year)) {
				thisYearList.add(c);
			}
		});
		SeriesUtil<NationalCarSalesTrends> util = new SeriesUtil<>();
		util.setLegendSolver(NationalCarSalesTrends::getNational);
		util.setFormSolver(NationalCarSalesTrends::getSale);
		return util.getSeries(thisYearList);
	}
}

